package com.sywb.chuangyebao.contract;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Channel;
import com.sywb.chuangyebao.view.fragment.ManusrciptStypeFragment;
import com.sywb.chuangyebao.widget.XTabLayout;

import org.bining.footstone.adapter.BasePagerAdapter;
import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;

import java.util.ArrayList;
import java.util.List;

public interface ManuscriptStypeContract {

    interface ManuscriptStypeIView extends IView {
        FragmentManager getUserFragmentManager();
    }

    class ManuscriptStypePresenter extends BasePresenter<ManuscriptStypeIView> {

        private ViewPager viewPager;
        private List<Fragment> fragmentList;
        private ChannelPagerFragmentAdapter pagerFragmentAdapter = null;
        private List<String> channels;

        @Override
        public void onStart() {
            fragmentList = new ArrayList<>();
            channels = new ArrayList<>();
        }

        /**
         * 初始化View
         *
         * @param type
         * @param viewPager
         * @param xTabLayout
         */
        public void initTabView(int type, ViewPager viewPager, XTabLayout xTabLayout) {
            this.viewPager = viewPager;
            switch (type) {
                //我的文章
                case 0:
                    initFiveOrFourView(type, mContext.getResources().getStringArray(R.array.manuscript_title));
                    break;
                //我的视频
                case 1:
                    initFiveOrFourView(type, mContext.getResources().getStringArray(R.array.manuscript_video_title));
                    break;
            }
            viewPager.setCurrentItem(0);
            xTabLayout.setupWithViewPager(viewPager);
        }


        private void initFiveOrFourView(int type, String[] titles) {
            for (int i = 0; i < titles.length; i++) {
                Fragment fragment = ManusrciptStypeFragment.newInstance(type, i);
                channels.add(titles[i]);
                fragmentList.add(fragment);
            }
            pagerFragmentAdapter = new ChannelPagerFragmentAdapter(mView.getUserFragmentManager(), fragmentList, channels);
            viewPager.setAdapter(pagerFragmentAdapter);
            viewPager.setOffscreenPageLimit(fragmentList.size());
        }

        class ChannelPagerFragmentAdapter extends FragmentStatePagerAdapter {

            private List<Fragment> fragments;
            private List<String> channels;

            public ChannelPagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> channels) {
                super(fragmentManager);
                this.fragments = fragments;
                this.channels = channels;
            }


            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return channels.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return channels == null ? "" : channels.get(position);
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        }
    }
}
